package com.web.boot.web.pan;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserResponse;
import com.web.cms.pan.facade.DeleteFileDataFacade;
import com.web.cms.pan.message.DeleteFileDataRequest;
import com.web.cms.pan.message.DeleteFileDataResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhengjin
 * @since 1.0 2018年05月02日
 */
@Controller("panDeleteDeleteFileDataController")
@Http(url = "/pan")
public class DeleteFileDataController
{
	@Autowired
	private DeleteFileDataFacade deleteFileDataFacade;
	@Autowired
	private UserFacade userFacade;

	private DictParser dictParser = DictParser.getInstance();

	public DeleteFileDataController()
	{
	}

	@Http(url = "/delete_file_data_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		DeleteFileDataRequest deleteFileDataRequest = request.getQueryObject(DeleteFileDataRequest.class);
		Page<DeleteFileDataResponse> page = deleteFileDataFacade.listPage(deleteFileDataRequest, Const.PAGE_START, Const.PAGE_SIZE);
		this.listUser(page.getObjectList());
		return View.createBuilder().setView("/pan/delete_file_data_list")
						.put("model", deleteFileDataRequest)
						.put("map", dictParser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/delete_file_data_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		DeleteFileDataRequest deleteFileDataRequest = request.getPostObject(DeleteFileDataRequest.class);
		int start = request.getPostInt("start", 1);
		Page<DeleteFileDataResponse> page = deleteFileDataFacade.listPage(deleteFileDataRequest, start, Const.PAGE_SIZE);
		this.listUser(page.getObjectList());
		return View.createBuilder().setView("/pan/delete_file_data_list_grid")
						.put("page", page)
						.build();
	}

	private void listUser(List<DeleteFileDataResponse> list)
	{
		if (list == null || list.isEmpty())
			return;

		List<Integer> idList = list.stream().map(DeleteFileDataResponse::getUserId).collect(Collectors.toList());
		Map<Integer, UserResponse> map = userFacade.getList(idList);
		for (DeleteFileDataResponse file : list)
		{
			UserResponse user = map.get(file.getUserId());
			if (user ==  null)
				continue;

			file.setUserMobile(user.getMobile());
			file.setUserName(user.getName());
			file.setUserNickname(user.getNickname());
		}
	}

	@Http(url = "/delete_file_data_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		String ids = request.getPost("ids");
		deleteFileDataFacade.delete(ids);
	}

}
